﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Reflection;
using BussinessLogicTecnoPez;
using Tecnopez.Globals;
using System.Text;

public partial class TecnoPezPopUp : System.Web.UI.UserControl
{
    private Object itemSelected;


    public void personalizarGestorAntesDelGetAll(personalizarGestor funcion)
    {
        Session["gestorAntes" + claveUnica] = funcion;
    }

    private string claveUnica
    {
        get
        {
            return this.ClientID + this.Page.Request.Url.Segments[this.Page.Request.Url.Segments.Length - 1].Replace(".", "");
        }
    }

    public Object ItemSelected
    {
        get 
        {
            if (string.IsNullOrEmpty(txtvalor.Value))
            {
                return null;
            }
            else
            {
                return Session["objElejido" + claveUnica];
            }
        }
        set 
        {
            if (value != null)
            {
                txtvalor.Value = value.ToString();
            }
            else
            {
                txtvalor.Value = string.Empty;
            } 
            itemSelected = value;
            Session["objElejido" + claveUnica] = value;
        }
    }
    
    private Type objType;

    public Type ObjType
    {
        get 
        { 
            return objType; 
        }
        set 
        { 
            objType = value;
            //para evitar problemas, la variable de sesion se
            //agrega cuando se setea la propiedad
            Session.Add(claveUnica, objType);
        }
    }

    public bool Enable
    {
        get { return btnAbrir.Disabled; }
        set { btnAbrir.Disabled = (!value); }
    }

    /// <summary>
    /// Indica si despues de seleccionar el item realiza postBack o no.
    /// </summary>
    private bool booDoPostBack = false;    
    public bool DoPostBack
    {
        get { return booDoPostBack; }
        set { booDoPostBack = value; }
    }

    /// <summary>
    /// Indica si requiere selección o no
    /// </summary>
    public bool TecnoPezActivateValidation
    {
        get { return this.rfvPopUp.Enabled; }
        set { this.rfvPopUp.Enabled = value; }
    }

    public string TecnoPezValidationGroup
    {
        get { return this.rfvPopUp.ValidationGroup; }
        set { this.rfvPopUp.ValidationGroup = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        string strFuncion;
        strFuncion = "function tomar_" + this.ClientID + "(text,ID){document.getElementById(ID).value  = text;" + "\n";
        if (this.DoPostBack)
            strFuncion = strFuncion + "__doPostBack('','');\n";
        strFuncion = strFuncion + "}";
        ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "tomar_" + this.ClientID, strFuncion, true);
        //__doPostBack('','');
        Abrir(this.Page);
    }

    public void Abrir(object sender)
    {
        Session.Add("PageSender", this);

        Page sen = (Page)sender;
        StringBuilder strScript = new StringBuilder();

        strScript.Append("open('/TecnoPezWeb/Globals/Search.aspx?id=");
        strScript.Append(txtvalor.ClientID);
        strScript.Append("&sessionName=" + claveUnica);
        strScript.Append("&function=" + this.ClientID);
        strScript.Append("','NewWindow','left=300,top=50,width=600,height=510,status=yes,resizable=yes,scrollbars=yes');");
        this.btnAbrir.Attributes.Add("onclick", strScript.ToString());
    }
}
